// 导入koa模块
const loaders = require('./loaders');
const Koa = require('koa');
const cors = require('koa2-cors');
const config = require('./config');
async function startServer () {
    // 创建koa的实例app
    const app = new Koa();
    // 设置允许跨域
    app.use(cors({
        origin: function (ctx) {
            return "*"; // 允许来自所有域名请求
        },
        exposeHeaders: ['WWW-Authenticate', 'Server-Authorization'],
        maxAge: 5,
        credentials: true,
        allowMethods: ['GET', 'POST', 'DELETE', 'OPTIONS'],
        allowHeaders: ['Content-Type', 'Authorization', 'Accept', 'x-requested-with', 'Access-Control-Allow-Origin'],
    }))
    await loaders({ koa: app });

    // 监听端口
    app.listen(config.port, err => {
        if (err) {
            console.log(err);
            return;
        }
        console.info(`Your server is ready !, time:`+new Date().toLocaleString());
        // console.info(`network：http://${config.host.split(',')[0]}\nlocal：http://${config.host.split(',')[1]}`);
        const appList = [
            {name: '服务地址network：', ip: `http://${config.host.split(',')[0]}`},
            {name: 'api文档地址local：', ip: `http://${config.host.split(',')[0]}/api-docs`},
            {name: '服务地址network：', ip: `http://${config.host.split(',')[1]}`},
            {name: 'api文档地址local：', ip: `http://${config.host.split(',')[1]}/api-docs`},
        ]
        console.table(
            appList,
            ['name', 'ip']
        )
    });
}
startServer();